<template>
  <div>
    <el-table :data="noneCompetitiveTableData" :span-method="objectSpanMethod" :cell-style="tableCellStyle" :row-class-name="tableRowClassName" size="mini" style="width: 100%">
      <el-table-column prop="locationName" label="比赛场地" :width="80" align="center" />
      <el-table-column prop="phaseName" label="进程" :min-width="80" align="center" />
      <el-table-column prop="unitCode" label="场次号" :min-width="80" align="center" />
      <el-table-column prop="eventName" label="比赛项目" :min-width="200" />
      <el-table-column prop="statusName" label="状态" :width="110">
        <template slot-scope="{ row }">
          <p v-if="row.phaseStatusCode == 'UnOfficial'" class="gl_color_success">{{ row.statusName }}</p>
          <p v-else>{{ row.statusName }}</p>
        </template>
      </el-table-column>
      <el-table-column label="参赛方" :min-width="220">
        <template slot-scope="{ row }">
          <PersonalImage v-if="row.registerTypeCode == 'I'" :imgurl="row.imagePath" :imgheight="'50px'" />
          <div v-else style="display:inline-block;">
            <img :src="getStaticFileUrl(row.imagePath)" class="person_img" @error="errorPhoto">
          </div>
          <div v-if="row.registerTypeCode == 'I'" class="gl_ml_10 gl_dis_ib" style="width: calc(100% - 80px); line-height: 22px; vertical-align: middle;">
            <el-popover placement="top" width="300" trigger="hover" :content="row.playerName">
              <div slot="reference" class="gl_ellipsis">{{ row.playerName ? row.playerName : '- -' }}</div>
            </el-popover>
            <el-popover placement="bottom" width="300" trigger="hover" :content="row.delegationName">
              <div slot="reference" class="gl_ellipsis">{{ row.delegationName ? row.delegationName : '- -' }}</div>
            </el-popover>
          </div>
          <div v-else class="gl_ml_10 gl_dis_ib" style="width: calc(100% - 80px); line-height: 22px; vertical-align: middle;">
            <el-popover placement="bottom" width="300" trigger="hover" :content="row.delegationName">
              <div slot="reference" class="gl_ellipsis">{{ row.delegationName ? row.delegationName : '- -' }}</div>
            </el-popover>
            <el-popover placement="bottom" width="300" trigger="hover" :content="row.memberNames">
              <div slot="reference" class="gl_ellipsis">{{ row.memberNames ? row.memberNames : '- -' }}</div>
            </el-popover>
          </div>
          <div class="gl_top" style="float:right; color: #fff">
            <span v-if="row.irmCode == 'DSQ'" style="padding: 2px" class="gl_bg_warning">弃</span>
            <span v-if="row.irmCode == 'DQB'" style="padding: 2px" class="gl_bg_error">失</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="totalScore" label="总得分" align="center" />
      <el-table-column prop="expressiveScore" label="表现力得分" :width="90" align="center" />
      <el-table-column prop="accuracyScore" label="准确率得分" :width="90" align="center" />
      <el-table-column prop="rank" label="比赛名次" align="center" />
      <el-table-column fixed="right" :label="$t('table.actions')" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="openNoneCompetitive(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="download(scope.row, 'RankList')">名次公告</el-button>
          <el-button :disabled="scope.row.noMoveTop" type="text" size="mini" @click="clickMoveUp(scope.row, scope.$index, noneCompetitiveTableData)">上移</el-button>
          <el-button :disabled="scope.row.noMoveBottom" type="text" size="mini" @click="clickMoveDown(scope.row, scope.$index, noneCompetitiveTableData)">下移</el-button>
        </template>
      </el-table-column>
    </el-table>
    <edit-none-competitive ref="editNoneCompetitive" @refreshPage="refreshPage()" />
  </div>
</template>
<script>
import { api_GET_DownLoadFileAddHeader } from '@/extend/api/file';
import { errorText } from '@/lib';
import { mapState, mapActions } from 'vuex';
import editNoneCompetitive from './editNoneCompetitive';
import defaultPersonImg from '@/assets/images/face_default.png';
import PersonalImage from '@/components/PersonalImage/index';
import {
  SiSTKWEventsManagementUpdateSessionByUnitManualInput // 更新编排手工
} from '@/lib/form/game/swaggerForm';
import {
  api_PUT_EventsManagementScheduleUnitSessionByUnitsManual
} from '@/extend/api/gameswaggerApi/app';
export default {
  name: 'NoneCompetitive',
  components: {
    editNoneCompetitive,
    PersonalImage
  },
  props: {
    noneCompetitiveTableData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      defaultPersonImg: defaultPersonImg
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict
    })
  },
  created() {
    this.getDict();
  },
  methods: {
    ...mapActions({ getDict: 'dict/getDict' }),
    // 定义需要合并的表格列
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex < 5) {
        const _row = this.setTable(this.noneCompetitiveTableData).one[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        };
      }
    },
    // 合并表格行
    setTable(tableData) {
      const spanArr = [];
      let concat = 0;
      tableData.forEach((item, index) => {
        if (index === 0) {
          spanArr.push(1);
        } else {
          if (item.unitId === tableData[index - 1].unitId) {
            // 需合并相同内容的判断条件
            spanArr[concat] += 1;
            spanArr.push(0);
          } else {
            spanArr.push(1);
            concat = index;
          }
        }
      });
      return {
        one: spanArr
      };
    },
    // 行高亮
    tableRowClassName({ row, rowIndex }) {
      if (row.isSameRank === true) {
        return 'warning_row';
      }
    },
    // 行高亮，兼容双人打分制
    tableCellStyle ({ row, column, rowIndex, columnIndex }) {
      if (columnIndex < 5 && row.unitResultList === true) {
        if (row.isSameRankDualScore === true) {
          return 'background: oldlace';
        }
      } else {
        return '';
      }
    },
    // 上移
    clickMoveUp (item, index, list) {
      if (index === 0) return;
      let key = '';
      if (item.unitId === list[index - 1].unitId) {
        key = index - 2;
      } else {
        key = index - 1;
      }
      const _currentVO = new SiSTKWEventsManagementUpdateSessionByUnitManualInput();
      _currentVO.id = item.unitId;
      _currentVO.sessionId = item.sessionId;
      _currentVO.orderInSession = list[key].orderInSession;
      const _preVO = new SiSTKWEventsManagementUpdateSessionByUnitManualInput();
      _preVO.id = list[key].unitId;
      _preVO.sessionId = list[key].sessionId;
      _preVO.orderInSession = item.orderInSession;

      this.submitMove([_currentVO, _preVO]);
    },
    // 下移
    clickMoveDown (item, index, list) {
      let key = '';
      if (item.unitId === list[index + 1].unitId) {
        key = index + 2;
      } else {
        key = index + 1;
      }
      const _currentVO = new SiSTKWEventsManagementUpdateSessionByUnitManualInput();
      _currentVO.id = item.unitId;
      _currentVO.sessionId = item.sessionId;
      _currentVO.orderInSession = list[key].orderInSession;
      const _nextVO = new SiSTKWEventsManagementUpdateSessionByUnitManualInput();
      _nextVO.id = list[key].unitId;
      _nextVO.sessionId = list[key].sessionId;
      _nextVO.orderInSession = item.orderInSession;
      this.submitMove([_currentVO, _nextVO]);
    },
    // 提交位置更改
    submitMove (list) {
      api_PUT_EventsManagementScheduleUnitSessionByUnitsManual(list).then(res => {
        this.refreshPage();
      }).catch(error => {
        this.$message({
          message: `移动失败！ ${error.error.message}`,
          type: 'error',
          showClose: true
        });
      });
    },
    refreshPage() {
      this.$emit('refreshPage');
    },
    // 编辑非竞技
    openNoneCompetitive(resultInfo) {
      console.log(resultInfo);
      this.$refs.editNoneCompetitive.open(resultInfo);
    },
    // 获取静态图片地址
    getStaticFileUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    errorPhoto() {
      const img = event.srcElement;
      img.src = this.defaultPersonImg;
    },
    /* 报表下载相关 */
    // 名次公告、对阵图
    download(row, reportName) {
      var that = this;
      if (reportName === 'RankList') {
        const tempUrl = `/api/app/events-management/report`;
        const fileName = '名次公告' + '.pdf';
        api_GET_DownLoadFileAddHeader(tempUrl, fileName, { ReportName: reportName, Format: 'PDF', EventId: row.eventId }).then(res => {
          //
        }).catch(error => {
          errorText(error).then(function(text) {
            that.$message({ type: 'error', message: text });
          });
        });
      } else if (reportName === 'Bracket') {
        const tempUrl = `/api/app/events-management/report`;
        const fileName = '对阵图' + '.pdf';
        api_GET_DownLoadFileAddHeader(tempUrl, fileName, { ReportName: reportName, Format: 'PDF', EventId: row.eventId, HasMatchCode: '1' }).then(res => {
          //
        }).catch(error => {
          errorText(error).then(function(text) {
            that.$message({ type: 'error', message: text });
          });
        });
      }
    }
  }
};
</script>
<style scoped>
.person_img {
  max-width: 50px;
  max-height: 50px;
  display: block;
}
</style>
<style>
.el-table .warning_row {
  background: oldlace;
}
/* .el-table tbody .warning_row.hover-row:hover>td { background-color: oldlace } */
</style>
